package iiris

import (
	"gitee.com/tietang/terrace-go/v2/base"
	"gitee.com/tietang/terrace-go/v2/base/starter/irpc"
	"github.com/kataras/iris/v12"
	log "github.com/sirupsen/logrus"
	"github.com/tietang/props/v3/kvs"
	"net/rpc"
)

//初始化自定义golang RPC,并结合iris和iris共用http端口
func InitRPCForIris(conf kvs.ConfigSource) *rpc.Server {
	log.Info("add iris gorpc support. ")
	server := irpc.RpcServer()

	Iris().Any(rpc.DefaultRPCPath, func(context iris.Context) {
		server.ServeHTTP(context.ResponseWriter(), context.Request())
	})
	Iris().Any(rpc.DefaultDebugPath, func(context iris.Context) {
		server.ServeHTTP(context.ResponseWriter(), context.Request())
	})

	return server

}

type IrisRpcStarter struct {
	base.BaseStarter
}

func (s *IrisRpcStarter) Setup(ctx base.StarterContext) {
	irpc.Server = InitRPCForIris(ctx.Props())
}
func (s *IrisRpcStarter) Start(ctx base.StarterContext) {}
func (s *IrisRpcStarter) Stop(ctx base.StarterContext)  {}
